<template>
	<view class="page">
		<!-- 头部 start -->
		<view class="cu-custom text-white bg-white fixed"
			:style="[{ height: CustomBar +54+ 'px'}]">
			<view class="cu-bar bg-white " :style="[{ height: CustomBar + 'px', paddingTop: StatusBar + 'px' }]">
				<view class="margin-left width-120 text-left" @tap="back"><text class="cuIcon-back"></text></view>
				<view class="">添加商品</view>
				<view class="right margin-right text-right width-120">
				</view>
			</view>
			<view class="flex align-center text-333 padding-lr padding-top-sm">
				<view class="flex align-center" style="max-width: 260rpx;" @tap="toggleTab('store')">
					<view class="margin-right-xs flex-shrink text-cut w85" >{{goods_shop_category||'全部分类'}}</view>
					<text class="cuIcon-unfold"></text>
				</view>
				<view class="text-333 flex-sub  margin-left">
					<view class="bg-f8  padding-lr padding-tb-14 flex align-center justify-between round">
						<text class="cuIcon-search text-999 margin-right-sm"></text>
						<input confirm-type="search" class="text-333 text-sm flex-sub" type="text" placeholder-class="text-999"
							v-model="keywords" :adjust-position="false" :hold-keyboard="false" placeholder="商品名称、商品货号 "
							@confirm="searchConfirm" />
						<text v-if="keywords.length != 0" class="cuIcon-close text-666 padding-right-xs"
							@tap="handleClear"></text>
					</view>
				</view>
			</view>
		</view>
		<!-- 头部 end -->
		<!-- 筛选 -->
		<!-- loading组件 -->
		<fu-loading v-if="isShowLoading"></fu-loading>
		<block v-else>
			<!--商品列表 start-->
			<view class="list padding-lr-24 relative padding-bottom-150 " :style="[{top:CustomBar+54+'px'}]">
				<view class="padding-top w100">
					<view class="flex align-center margin-bottom-sm bg-white padding-sm radius-16"
						v-for="(item, index) in lists" :key="index">
						<view @tap="check(item, index)" class="margin-right-16" v-if="isEditor">
							<checkbox v-if="isEditor" class="round theme" :class="item.delChecked ? 'checked' : ''"
								:checked="item.delChecked ? true : false" style="transform: scale(0.7);"></checkbox>
						</view>
						<view class="w100">
							<view class="goods-info flex align-center " @tap="toDetail(item, index)">
								<view class=" relative margin-right-sm flex-shrink fu-block-200 radius-16 overHidden">
									<view class="box-kong padding-lr-16 padding-tb-4 text-white text-sm">
										{{item.port_name}}
									</view>
									<fu-image :src="item.thumb" mode="aspectFill"></fu-image>
								</view>
								<view class="height-200 flex  justify-between flex-direction w80">
									<view>
										<view class="text-df text-cut-2">{{ item.name }}
										</view>
										<view class="text-sm text-999 margin-top-sm sku_name">
											<view class=" ">
												<text>商品货号：{{ item.sn }}</text>
											</view>
										</view>
									</view>
									<view class="desc-number flex justify-between align-center">
										<view class="text-lg text-prices flex-shrink" @tap="toDetail(item, index)">
											<text class="text-price"></text>
											<text>{{ item | filterGoodsPrice }}</text>
										</view>
										<view class="text-sm text-999 text-cut"> 销量{{item.sales_sum||0}} </view>
										<view class="text-sm text-999 text-cut"> 库存{{item.stock||0}} </view>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="action-bar padding-lr cu-bar  bg-white flex justify-between" v-if="isEditor&&lists.length>0">
				<view class="flex flex-sub align-center   justify-between">
					<view class="flex justify-between align-center" @tap="checkAll">
						<checkbox v-if="isEditor" class="round theme" :class="delAllChecked ? 'checked' : ''"
							:checked="delAllChecked ? true : false" style="transform: scale(0.7)"></checkbox>
						<checkbox v-if="!isEditor" class="round theme aaaaa" :class="allChecked ? 'checked' : ''"
							:checked="allChecked ? true : false" style="transform: scale(0.7)"></checkbox>
						<text class="margin-left-sm text-df text-666">全选</text>
					</view>
				</view>
				<view class="flex align-center">
					<button class="cu-btn btn-settlement radius-100 bg-theme text-white text-df " @click="confirmAdd">
						确认选择
					</button>
				</view>
			</view>
			<!--商品列表 end-->

			<fu-empty-ui v-if="lists.length == 0 && isInit"></fu-empty-ui>
		</block>
		<!-- 二级联动 店铺商品分类 -->
		<w-picker :visible.sync="storeVisible" mode="linkage" :options="storeTypeList" :level="2" themeColor="#FF6464"
			default-type="id" @confirm="onConfirm($event,'store')" ref="store">
		</w-picker>

		<!-- 断网检测组件 -->
		<fu-notwork></fu-notwork>
	</view>
</template>
<script>
	// 基础筛选与高级筛选组件
	import fuFilterDropdown from '../../../goods/components/fu-filter-dropdown/fu-filter-dropdown.vue';
	// 楼层效果混入js
	import floor from '@/common/mixin/floor';
	export default {
		components: {
			fuFilterDropdown,
		},
		mixins: [floor],
		data() {
			return {
				timer: null, // 定时器
				isEditor: true, // 管理 or 完成
				type: 1, // 悬浮按钮状态 1 页码 2 返回顶部
				isShow: false, //是否显示按钮
				totalSize: 0, // 悬浮按钮总页码
				currentPage: 1, // 当前页页码
				storeVisible: false, //分类
				typeList: [], //分类
				keywords: '',
				isShowLoading: true, // 是否显示loading
				goods_shop_category: '', //分类
				goods_shop_categoryId: '', //
				keywords: '', // 搜索的文本
				placeholder: global.i18n['请输入商品关键字'], // 默认文本
				model: 'block', // 布局样式
				lists: [], // 商品列表
				storeTypeList: [], //店铺商品分类
				cateName: '', //分类，名字
				cid: '', // 分类id(点击分类跳转过来必传)
				isInit: false, // 是否请求过数据
				status: 'more', // 加载状态
				page: 1, // 页数
				size: 10, // 页条数
				sorts: '1', // 条件
				order: 'asc', // 排序 asc: 正序 desc: 倒序
				advancedSearch: null, // 高级搜索对象
				isShowStock: false, //库存弹框
				isShowAdd: false, //补仓弹框
				stockIpt: '', //库存数量
				stockItem: '', //选择的库存
				step: 1, // 1 展示配件商品 2 删除 3 添加 4 添加之后列表
				pageType: '', //repair 补仓添加商品
				goods_ids: '', //商品id
			};
		},
		computed: {
			// 管理模式全选状态
			delAllChecked() {
				let list = this.lists;
				let bool = true;
				list.forEach(item => {
					// if(item.stock == 0) return ;
					if (item.delChecked == false) {
						bool = false;
					}
				});
				return bool;
			},
			// 正常模式全选状态
			allChecked() {
				let list = this.lists;
				let bool = true;
				list.forEach(item => {
					if (item.stock == 0) return;
					if (item.is_valid == 1) return;
					if (item.checked == false) {
						bool = false;
					}
				});
				return bool;
			},
			// 选择的商品数量
			totalNumber() {
				let list = this.lists;
				let total = 0;
				list.forEach(item => {
					if (item.checked == true) {
						total += Number(item.num);
					}
				});
				return total;
			},
		},

		onLoad(options) {
			/***
			 * @keyWords 关键词
			 * @cid 分类id
			 **/
			const {
				goods_ids = '', pageType
			} = options;
			if (goods_ids) {
				this.goods_ids = goods_ids;
			}
			this.pageType = pageType
			this.getBusinessTypeList()
			this.handleSearch(); // 搜索方法
		},
		onReachBottom() {
			this.handleSearch(); // 搜索方法
		},
		methods: {
			toggleTab(ref) {
				this.$refs[ref].show();
			},
			// 店铺商品分类 二级
			getBusinessTypeList() {
				this.$api.post('/v1/6356329f1f82a', {
					partner_id: global.userInfo && global.userInfo.id
				}).then(res => {
					let data = res.data;
					if (data.code == 1) {
						data.data.map(item => {
							item.label = item.name
							item.value = item.id
							item.children = item.child
							if (item.child_count > 0) {
								item.children.map(item1 => {
									item1.label = item1.name
									item1.value = item1.id
									item1.children = item1.child
								})
							}
						})
						let list = data.data;
						// 判断没有二级分类删除此分类
						list.forEach((item, index) => {
							if (item.child_count <= 0) {
								list.splice(index, 1);
							}
						});
						let arr0={
							label:'全部分类',
							value:0,
							children:[{
								label:'全部分类',
								value:0
							}],
						}
						list.unshift(arr0)
						console.log(list ,'this.storeTypeList ');
						this.storeTypeList = list
					} else {
						this.$message.info(res.data.msg);
					}
				}).catch(err => {
					this.$message.info("服务器错误，请稍后重试");
				});
			},

			onConfirm(result, type) {
				console.log(result, type);
				this.goods_shop_category = result.obj&&result.obj.col2.label
				this.goods_shop_categoryId = result.value[1]
				this.page = 1;
				this.status = 'more';
				this.handleSearch();

			},
			// 商品选中状态处理
			check(item, i) {
				if (this.isEditor) {
					item.delChecked = !item.delChecked;
				} else {
					this.lists[i].checked = !this.lists[i].checked;
					this.cacheCartChecked();
				}
			},
			checkfor() {
				//选中状态，页面关闭的时候全部为false
				let _type = false;
				this.lists.forEach(item => {
					item.checked == false;
					item.delChecked == false;
				});
				this.allChecked = false;
				this.delAllChecked = false;
			},
			//全选|取消全选
			checkAll() {
				if (this.isEditor) {
					const bool = !this.delAllChecked;
					this.lists.forEach(item => {
						item.delChecked = bool;
					});
				} else {
					const bool = !this.allChecked;
					console.log('unEditor', bool)
					this.lists.forEach(item => {
						item.checked = bool;
					});
				}
			},
			openPop(item, index) {
				this.isShowStock = true
				this.stockItem = item
			},

			// 选择商品
			confirmAdd() {
				const list = this.lists;
				let ids = [],
					goodsInfo = [];
				list.forEach(item => {
					if (item.delChecked) {
						ids.push(item.id);
						goodsInfo.push(item)
					}
				});
				console.log(ids, 'ids');
				if (ids.length == 0) return this.$message.info(this.i18n['未选择商品']);
				if(this.pageType=='repair'){ //补仓添加商品
					uni.$emit('repairGoodsInfo', ids)
				}else{
					uni.$emit('accessGoodsInfo', ids)
				}
				uni.navigateBack()
			},
			//删除选中
			delChoose() {
				const list = this.lists;
				let ids = [];
				list.forEach(item => {
					if (item.delChecked) {
						ids.push(item.id);
					}
				});
				console.log(ids, 'ids');
				if (ids.length == 0) return this.$message.info(this.i18n['未选择商品']);
				this.$util.showModal({
					title: this.i18n['提示'],
					content: '商品出售中，删除后未购买的用户不可见，已购买的用户不受影响 确定删除当前内容？',
					confirmColor: this.$store.state.themeColor,
					success: res => {
						if (res.confirm) {
							this.$api
								.post(global.apiUrls.delCart, {
									cart_ids: ids.join(',')
								})
								.then(res => {
									console.log('res: ' + JSON.stringify(res));
									this.getCartData();
									this.step = 2
								})
								.catch(err => {
									console.log('err: ' + JSON.stringify(err));
								});
						}
					}
				});
			},
			toDetail(item, index) {
				this.$urouter.navigateTo({
					url: '/pages/goods/goodsdetail/goods-detail/index',
					params: {
						"id": item.id,
					}
				})
			},
			/**
			 * @description 切换搜索条件
			 * @param {Object} e input框事件源
			 */
			searchConfirm(e) {
				if (e && e.detail.value.trim() == '') {
					this.$message.info(this.i18n['请输入要搜索的内容']);
					return;
				}
				// this.cid = 0;
				this.page = 1;
				this.status = 'more';

				this.handleSearch();
				uni.hideKeyboard();
			},
			/**
			 * @func 搜索
			 */
			handleSearch() {
				if (this.status != 'more') return;
				this.status = 'loading';
				let data = {
					type: 'sale', //已上架				
					page: this.page,
					list_rows: this.size,
					goods_ids: this.goods_ids,
					goods_shop_category: this.goods_shop_categoryId
				};
				// 判断关键字或者placeholder内容
				if (this.placeholder.trim() || this.goods_ids.trim()) {
					data.keyword = this.keywords;
				}
				if (this.page == 1) {
					this.isShowLoading = true;
				}
				// 获取商品列表接口
				this.$api
					.post('/v1/63db775cd4a5a', data)
					.then(res => {
						this.isShowLoading = false;
						if (res.data.code == 1) {
							let totalSize = res.data.data.total;
							this.totalSize = Math.ceil(totalSize / 10); // 悬浮按钮总页码
							this.currentPage = this.page; //悬浮当前页面
							let list = res.data.data.data;
							if (this.page == 1) this.lists = [];
							list = list.map(item => {
								let obj = {};
								obj = {
									...item,
									delChecked: false
								}
								return obj;
							})
							this.lists = this.lists.concat(list);
							if (this.lists.length >= totalSize) {
								this.status = 'noMore';
							} else {
								this.status = 'more';
								this.page++;
							}
							this.isInit = true;
						} else {
							this.$message.error(res.data.msg);
						}
					})
					.catch(err => {
						this.isShowLoading = false;
					});
			},
			// 清除关键字
			handleClear() {
				this.keywords = '';
				this.page = 1;
				this.status = 'more';

				this.handleSearch();
				uni.hideKeyboard();
			},
			// 返回
			back() {
				uni.navigateBack();
			},
		
		}
	};
</script>

<style lang="scss" scoped>
	page {
		height: 100%;
	}

	.bg-top {
		background: linear-gradient(90deg, #FFBE65 0%, #F97F33 100%);
	}

	.fixed {
		position: fixed;
		top: 0;
		width: 100%;
		z-index: 100;
	}

	.page {
		min-height: 100%;
		/* background-color: #ffffff; */
	}

	.head-top {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
	}

	.nav-bar-wrap {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
	}

	.sort-wrap {
		position: fixed;
		left: 0;
		right: 0;
	}

	.height-100 {
		height: 100rpx;
	}

	.block-180 {
		width: 180rpx;
		min-width: 180rpx;
		height: 180rpx;
		min-height: 180rpx;
		position: relative;

		image {
			width: 100%;
			height: 100%;
			border-radius: 8rpx;
		}

		.null {
			position: absolute;
			top: 0;
			right: 0;
			bottom: 0;
			left: 0;
			border-radius: 8rpx;
			background-color: rgba(0, 0, 0, 0.45);
			color: #ffffff;
			font-size: 36rpx;
			text-align: center;
			line-height: 180rpx;
		}
	}

	.list-item+.list-item {
		border-top: 1rpx solid #eeeeee;
	}

	.overlay-animate {
		position: fixed;
	}

	.float-action {
		width: 72rpx;
		height: 72rpx;
		border-radius: 50%;
		// background-color: rgba(165, 165, 165, .85);
		position: fixed;
		right: 20rpx;
		bottom: 280rpx;
		z-index: 996;
		border: 1rpx solid #cccccc;

		.cuIcon-cart {
			color: #cccccc;
		}
	}

	.animate {
		position: fixed;
		border-radius: 50%;
		z-index: 995;
		transform-origin: 50% 50%;
		transition: transform linear 0.5s, left linear 0.5s, top cubic-bezier(0.3, -0.2, 1, 0) 0.5s !important;

		image {
			width: 100rpx;
			height: 100rpx;
			border-radius: 50%;
		}
	}

	// 商品卡片line
	.fu-goods-line {
		/* min-height: 570rpx; */
		padding: 24rpx 0;
		border-radius: 24rpx;
		/* background-color: #ffffff; */
		// margin-top: 20rpx;
		display: flex;

		.block-246 {
			padding: 24rpx 24rpx 0;

			.img {
				width: 172rpx;
				height: 172rpx;
				border-radius: 6rpx;
				background-color: #eeeeee;
			}
		}

		.content {
			flex: 1;
			overflow: hidden;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
		}
	}

	// 商品卡片block
	.fu-goods-block {
		overflow: hidden;
		margin-bottom: 20rpx;
		padding: 0 10rpx;

		.wrap-box {
			background-color: #ffffff;
			border-radius: 16rpx;

			.square-wrap {
				width: 100%;
				height: 0;
				padding-bottom: 100%;
				box-sizing: border-box;
				position: relative;

				.square-box {
					position: absolute;
					left: 0;
					top: 0;
					right: 0;
					bottom: 0;
					border-radius: 24rpx 24rpx 0 0;

					image {
						width: 100%;
						height: 100%;
						border-radius: 24rpx 24rpx 0 0;
					}
				}
			}

			.image {
				width: 100%;
				border-radius: 24rpx 24rpx 0 0;
			}
		}

		.content {
			padding: 20rpx;
		}
	}

	.item-activity-label {
		padding: 2rpx 5rpx;
		/* margin: 5rpx; */
		font-size: 24rpx;
		border-radius: 6rpx;
		border: 1px solid;
		margin: 0rpx 1px 0px;
		display: inline-block;
		transform: scale(0.8);
	}

	.padding-lr-24 {
		padding: 0 24rpx;
	}

	.padding-lr-4 {
		padding: 0 4rpx;
	}

	.search-form {
		margin-left: 12rpx;
	}

	.item-goods-label {
		padding: 2rpx 8rpx;
		color: #ff6464;
		font-size: 24rpx;
		margin-right: 6rpx;
		background: rgba(255, 100, 100, 0.1);
		/* opacity: 0.1; */
		border-radius: 4rpx;
	}

	.item-activity-label {
		padding: 0 8rpx;
		height: 32rpx;
		line-height: 30rpx;
		border: 1px solid #fa2033;
		opacity: 1;
		border-radius: 4rpx;
		font-size: 20rpx;
		font-family: PingFang SC;
		font-weight: 400;
		color: #fa2033;
		margin-right: 16rpx;

		&:nth-of-type(1) {
			border: 1px solid #0a7ce6;
			color: #0a7ce6;
		}
	}

	.sales-number {
		margin-top: 16rpx;
	}

	.through-price {
		margin-left: 16rpx;
		color: #bfbfbf;
	}

	.box-kong {
		position: absolute;
		border-radius: 16rpx 0 16rpx 0;
		left: 0;
		top: 0;
		z-index: 99;
		background: rgba(0, 0, 0, 0.6);
	}

	.action-bar {
		position: fixed;
		bottom: 0 !important;
		width: 100%;
		z-index: 999;
	}
</style>
